home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Ports / mac / menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-06  |  4.1 KB  |  271 lines  |  [TEXT/????]

  1. /* MAC STDWIN -- MENUS. */
  2.  
  3. /* XXX Ought to find a way to allow the THINK C console menus
  4.    to work when the console is active */
  5.  
  6. #include "macwin.h"
  7. #ifdef MPW
  8. #include <Fonts.h>
  9. #include <Menus.h>
  10. #include <ToolUtils.h>
  11. #endif
  12. #ifdef THINK_C_PRE_5_0
  13. #include <MenuMgr.h>
  14. #endif
  15.  
  16. #define MENUID(mp) ((*((MenuHandle)(mp)))->menuID)
  17.  
  18. bool _wmenuhilite;        /* Set if HiliteMenu(0) needed */
  19.  
  20. static bool deflocal= FALSE;    /* Default menu state */
  21.  
  22. static int firstlocal;        /* First local menu in menu bar */
  23.  
  24. /* Function prototypes */
  25.  
  26. STATIC void addtobar _ARGS((struct menubar *mbp, MENU *mp));
  27. STATIC void delfrombar _ARGS((struct menubar *mbp, MENU *mp));
  28.  
  29. static void
  30. addtobar(mbp, mp)
  31.     struct menubar *mbp;
  32.     MENU *mp;
  33. {
  34.     int i;
  35.     
  36.     for (i= 0; i < mbp->nmenus; ++i) {
  37.         if (mp == mbp->menulist[i])
  38.             return; /* Already attached */
  39.     }
  40.     L_APPEND(mbp->nmenus, mbp->menulist, MENU *, mp);
  41. }
  42.  
  43. static void
  44. delfrombar(mbp, mp)
  45.     struct menubar *mbp;
  46.     MENU *mp;
  47. {
  48.     int i;
  49.     
  50.     for (i= 0; i < mbp->nmenus; ++i) {
  51.         if (mp == mbp->menulist[i]) {
  52.             L_REMOVE(mbp->nmenus, mbp->menulist, MENU *, i);
  53.             break;
  54.         }
  55.     }
  56. }
  57.  
  58. MENU *
  59. wmenucreate(id, title)
  60.     int id;
  61.     char *title;
  62. {
  63.     MENU *mp= (MENU *)NewMenu(id, PSTRING(title));
  64.     
  65.     if (!deflocal) {
  66.         InsertMenu(mp, firstlocal);
  67.         DrawMenuBar();
  68.     }
  69.     return mp;
  70. }
  71.  
  72. void
  73. wmenudelete(mp)
  74.     MENU *mp;
  75. {
  76.     WindowPtr w;
  77.     
  78.     for (w= FrontWindow(); w != NULL;
  79.         w= (WindowPtr)((WindowPeek)w)->nextWindow) {
  80.         WINDOW *win= whichwin(w);
  81.         if (win != NULL)
  82.             delfrombar(&win->mbar, mp);
  83.     }
  84.     DeleteMenu(MENUID(mp));
  85.     DrawMenuBar();
  86.     DisposeMenu(mp);
  87. }
  88.  
  89. int
  90. wmenuadditem(mp, text, shortcut)
  91.     MENU *mp;
  92.     char *text;
  93.     int shortcut;
  94. {
  95.     char buf[256];
  96.     int item;
  97.     
  98.     if (text == NULL || *text == EOS)
  99.         strcpy(buf, "(-");
  100.     else
  101.         strcpy(buf, "x");
  102.     if (shortcut >= 0) {
  103.         char *p= buf + strlen(buf);
  104.         *p++= '/';
  105.         *p++= shortcut;
  106.         *p= EOS;
  107.     }
  108.     AppendMenu(mp, PSTRING(buf));
  109.     item= CountMItems(mp);
  110.     if (text != NULL && *text != EOS)
  111.         SetItem(mp, item, PSTRING(text));
  112.     return item-1;
  113. }
  114.  
  115. void
  116. wmenusetitem(mp, item, text)
  117.     MENU *mp;
  118.     int item;
  119.     char *text;
  120. {
  121.     ++item;
  122.     if (text == NULL || *text == EOS)
  123.         DisableItem(mp, item);
  124.     else {
  125.         EnableItem(mp, item);
  126.         SetItem(mp, item, PSTRING(text));
  127.     }
  128. }
  129.  
  130. void
  131. wmenuenable(mp, item, flag)
  132.     MENU *mp;
  133.     int item;
  134.     int flag;
  135. {
  136.     ++item;
  137.     if (flag)
  138.         EnableItem(mp, item);
  139.     else
  140.         DisableItem(mp, item);
  141. }
  142. void
  143. wmenucheck(mp, item, flag)
  144.     MENU *mp;
  145.     int item;
  146.     int flag;
  147. {
  148.     ++item;
  149.     CheckItem(mp, item, flag);
  150. }
  151.  
  152. void
  153. wmenuattach(win, mp)
  154.     WINDOW *win;
  155.     MENU *mp;
  156. {
  157.     addtobar(&win->mbar, mp);
  158.     if (win == active) {
  159.         InsertMenu(mp, 0);
  160.         DrawMenuBar();
  161.     }
  162. }
  163.  
  164. void
  165. wmenudetach(win, mp)
  166.     WINDOW *win;
  167.     MENU *mp;
  168. {
  169.     delfrombar(&win->mbar, mp);
  170.     if (win == active) {
  171.         DeleteMenu(MENUID(mp));
  172.         DrawMenuBar();
  173.     }
  174. }
  175.  
  176. void
  177. wmenusetdeflocal(local)
  178.     bool local;
  179. {
  180.     deflocal= local;
  181. }
  182.  
  183. /* Interface routines for the rest of the library. */
  184.  
  185. void
  186. initmbar(mb)
  187.     struct menubar *mb;
  188. {
  189.     L_INIT(mb->nmenus, mb->menulist);
  190. }
  191.  
  192. void
  193. killmbar(mb)
  194.     struct menubar *mb;
  195. {
  196.     L_DEALLOC(mb->nmenus, mb->menulist);
  197. }
  198.  
  199. void
  200. addlocalmenus(win)
  201.     WINDOW *win;
  202. {
  203.     int i;
  204.     
  205.     firstlocal= 0;
  206.     for (i= 0; i < win->mbar.nmenus; ++i) {
  207.         if (firstlocal == 0)
  208.             firstlocal= MENUID(win->mbar.menulist[i]);
  209.         InsertMenu(win->mbar.menulist[i], 0);
  210.     }
  211.     if (i > 0)
  212.         DrawMenuBar();
  213. }
  214.  
  215. void
  216. rmlocalmenus(win)
  217.     WINDOW *win;
  218. {
  219.     int i;
  220.     
  221.     firstlocal= 0;
  222.     for (i= 0; i < win->mbar.nmenus; ++i)
  223.         DeleteMenu(MENUID(win->mbar.menulist[i]));
  224.     if (i > 0)
  225.         DrawMenuBar();
  226. }
  227.  
  228. void
  229. _wdo_menu(ep, menu_item)
  230.     EVENT *ep;
  231.     long menu_item;
  232. {
  233.     int id= HiWord(menu_item);
  234.     int item= LoWord(menu_item);
  235.     
  236.     if (id == 0)
  237.         return;
  238.     if (id == APPLE_MENU) {
  239.         if (item == 1)
  240.             do_about();
  241.         else {
  242.             char name[256];
  243.             GetItem(GetMHandle(id), item, name);
  244.             (void) OpenDeskAcc(name);
  245.         }
  246.         HiliteMenu(0);
  247.     }
  248.     else {
  249.         ep->type= WE_MENU;
  250.         ep->u.m.id= id;
  251.         ep->u.m.item= item-1;
  252.         _wmenuhilite= TRUE;
  253.     }
  254. }
  255.  
  256. /* Call this routine exactly once to initialize the Apple menu. */
  257.  
  258. char *about_item= "About STDWIN...";
  259.  
  260. void
  261. setup_menus()
  262. {
  263.     static char applename[2]= {appleMark, EOS};
  264.     MenuHandle m;
  265.     
  266.     m= (MenuHandle)wmenucreate(APPLE_MENU, applename);
  267.     AppendMenu(m, PSTRING(about_item));
  268.     AppendMenu(m, PSTRING("(-"));
  269.     AddResMenu(m, 'DRVR');
  270. }
  271.